package com.breaktian.assemble.refresh.superrefresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.breaktian.assemble.R;
import com.breaktian.assemble.utils.LogUtil;
import com.breaktian.assemble.utils.ThreadUtil;

/**
 * Created by tiankeke on 2017/6/20.
 */

public class PullDownRefreshLayout extends SuperSwipeRefreshLayout {
    private Context mContext;
    private ProgressBar headProgressBar;
    private TextView headTextView;
    private ImageView headImageView;
    private OnPullDownRefreshListener mPullDownRefreshListener;

    public PullDownRefreshLayout(Context context) {
        super(context);
        init(context);
    }

    public PullDownRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    private void init(Context context) {
        this.mContext = context;
        setHeaderView(createHeadView());

        setOnPullRefreshListener(new OnPullRefreshListener() {
            @Override
            public void onRefresh() {
                LogUtil.d("onRefresh");
                headTextView.setText("正在刷新");
                headImageView.setVisibility(View.GONE);
                headProgressBar.setVisibility(View.VISIBLE);
                if(mPullDownRefreshListener!=null){
                    mPullDownRefreshListener.onPullDown();
                }

            }

            @Override
            public void onPullDistance(int distance) {
                LogUtil.d("onPullDistance");
            }

            @Override
            public void onPullEnable(boolean enable) {
                LogUtil.d("onPullEnable :"+enable);
                headTextView.setText(enable ? "松开刷新" : "下拉刷新");
                headImageView.setVisibility(View.VISIBLE);
                headImageView.setRotation(enable ? 180 : 0);
            }
        });


    }

    /**
     * 设置刷新监听器
     * */
    public void setOnPulldownRefreshListener(OnPullDownRefreshListener pullDownRefreshListener){
        this.mPullDownRefreshListener = pullDownRefreshListener;
    }

    public interface OnPullDownRefreshListener{
        void onPullDown();
    }

    View createHeadView(){
        View headView = LayoutInflater.from(mContext).inflate(R.layout.bm_refresh_layout_head, null);
        headProgressBar = (ProgressBar)headView.findViewById(R.id.pb_view);
        headTextView = (TextView)headView.findViewById(R.id.text_view);
        headImageView = (ImageView)headView.findViewById(R.id.image_view);
        return headView;
    }

    /**
     * 结束刷新
     * */
    public void stopPullDownRefresh(){
        ThreadUtil.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                setRefreshing(false);
                headProgressBar.setVisibility(View.GONE);
            }
        });
    }


}
